//
//  SEDoctorDetailReserveServiceView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/13.
//

import UIKit

protocol SEDoctorDetailReserveServiceViewDelegate: NSObjectProtocol {
    func doctorDetailReserveServiceViewSectionHeaderTap()
}

@IBDesignable class SEDoctorDetailReserveServiceView: UIView {
    
    var view: UIView!


    @IBOutlet private weak var containerView: UIView! {
        didSet {
            containerView.backgroundColor = UIColor(hex: "#E9F3FD").withAlphaComponent(0.7)
            containerView.layer.cornerRadius = 10
            containerView.layer.borderWidth = 1
            containerView.layer.borderColor = UIColor(hex: "#DDECFB").cgColor
            containerView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.estimatedRowHeight = 44.0
            tableView.rowHeight = UITableView.automaticDimension
            tableView.backgroundColor = .clear
            tableView.tableFooterView = UIView()
            tableView.delegate = self
            tableView.dataSource = self
            tableView.layer.cornerRadius = 10
            tableView.clipsToBounds = true
            
            tableView.register(SEDoctorDetailReserveServiceCell.nibForCell(),
                               forCellReuseIdentifier: SEDoctorDetailReserveServiceCell.reuseIdentifier())
        }
    }

    var viewModel = SEDoctorDetailViewModel()
    weak var delegate: SEDoctorDetailReserveServiceViewDelegate?
    weak var dateViewDelagate: SERegistrationDoctorDateViewDelegate?
    
    // MARK: - life cycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }

    func updateData(_ viewModel: SEDoctorDetailViewModel) {
        self.viewModel = viewModel
        tableView.reloadData()
    }
    
    private func setupUI() {
        view = loadViewFromNib()
        view.backgroundColor = .white
    }
    
    // MARK: - load nib for view
    
    private func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName(), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)

        return view
    }

    private func nibName() -> String {
        return String(describing: type(of: self))
    }
}

// MARK: - UITableViewDataSource

extension SEDoctorDetailReserveServiceView: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.model.shifts.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEDoctorDetailReserveServiceCell.reuseIdentifier()) as! SEDoctorDetailReserveServiceCell
        cell.dateViewDelegate = self
        cell.updateWithViewModel(viewModel.model.shifts[indexPath.row])
        return cell
    }
}

// MARK: - UITableViewDelegate

extension SEDoctorDetailReserveServiceView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UINib.loadViewFrom(SEDoctorDetailReserveServiceSectionHeader.self)
        header.updateWithViewModel(viewModel.model.shifts[section])
        header.tappedClosure = {
            let model = self.viewModel.model.shifts[section]
            let isOpen = !model.isOpen
            self.viewModel.model.shifts[section].isOpen = isOpen
            self.tableView.reloadData()
            self.delegate?.doctorDetailReserveServiceViewSectionHeaderTap()
        }
        return header
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = UIView()
        footer.backgroundColor = .clear
        return footer
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 34
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        switch indexPath.section {
//        case 0:
//            return 152
//        default:
//            return 0
//        }
//    }
}

extension SEDoctorDetailReserveServiceView: SERegistrationDoctorDateViewDelegate {
    func registrationDoctorDateViewBtnAction(model: SERegistrationDoctorShift) {
        dateViewDelagate?.registrationDoctorDateViewBtnAction(model: model)
    }
}
